#include <stdio.h>
#include <iostream>
#include <mysql/mysql.h>
// int main()
// {
//     // printf("mysql client Version: %s\n", mysql_get_client_info());
//     printf("mysql client version:%s\n",mysql_get_client_info());
//     return 0;
// }

const std::string host = "127.0.0.1";
const std::string user = "bqx";
const std::string password = "12345678";
const std::string db = "d1";
const unsigned int port = 3306;

int main()
{
    MYSQL *my = mysql_init(nullptr);
    if (my == nullptr)
    {
        std::cerr << "mysql_init error" << std::endl;
        return 1;
    }
    std::cout << "mysql_init success" << std::endl;

    MYSQL *mrc = mysql_real_connect(my, host.c_str(), user.c_str(), password.c_str(), db.c_str(), port, nullptr, 0);
    if (mrc == nullptr)
    {
        std::cerr << "mysql_real_connect fail" << std::endl;
    }
    else
    {
        std::cout << "mysql_real_connect success" << std::endl;
    }
    std::string sq1;
    while (true)
    {
        std::cout << ">>>" << std::endl;
        if (!std::getline(std::cin, sq1)||sq1 == "quit")
        {
            std::cout<<"bye bye"<<std::endl;
            break;
        }
        int n = mysql_query(my, sq1.c_str());
        if(n==0)
            std::cout<<"success:"<<n<<std::endl;
        else
            std::cerr<<"fail:"<<n<<std::endl;
    }
    MYSQL_RES *result = mysql_store_result(my);
    // std::cout << "result: " << result->data << std::endl;
    int rows=mysql_num_rows(result);
    int fields=mysql_num_fields(result);
    // std::cout<<"mysql_num_fields:"<<fields<<std::endl;
    MYSQL_FIELD*sqf=mysql_fetch_field(result);
    for(int i=0;i<fields;i++)
    {
        std::cout<<sqf[i].name<<" ";
    }
    std::cout<<std::endl;
    MYSQL_ROW line;
    for(int i=0;i<rows;i++)
    {
        line=mysql_fetch_row(result);
        for(int j=0;j<fields;j++)
        {
            std::cout<<line[j]<<" ";
        }
        std::cout<<std::endl;
    
    }

    mysql_close(my);
    // mysql_free_result(result);

    return 0;
}